package com.yunsc.admin.controller.media;

import com.yunsc.common.base.YunscStoreUser;
import com.yunsc.common.base.YunscUser;
import com.yunsc.common.utils.Result;
import com.yunsc.common.utils.SecurityUtil;
import com.yunsc.media.entity.YunscUploadFileEntity;
import com.yunsc.media.service.UploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-02-25 14:21
 */
@Api(tags = "文件接口", value = "文件上传接口")
@Slf4j
@RestController
@RequestMapping("upload")
public class UploadFileController {

    @Resource
    private UploadService uploadService;

    @ApiOperation(value = "用户端上传图片", notes = "用户端上传图片")
    @PostMapping("/shop/image")
    public Result<?> uploadImage(HttpServletRequest httpServletRequest) {
        YunscUser user = SecurityUtil.getCurrentUser();
        // 获取MultiPart请求
        MultipartHttpServletRequest multiPartRequest = (MultipartHttpServletRequest) httpServletRequest;
        // 获取所有上传的文件
        Map<String, MultipartFile> fileMap = multiPartRequest.getFileMap();
        MultipartFile file = fileMap.get("file");
        // 上传文件
        // （file 文件、groupId 组id、channel 渠道、folder 文件夹、userId 用户id、storeId 商户id）
        YunscUploadFileEntity uploadFile = uploadService.
                uploadFileEntity(file, 0, 20, null, user.getUserId(), user.getStoreId());
        Map<String, Object> data = new HashMap<>();
        data.put("fileInfo", uploadFile);
        return Result.ok(data);
    }

    @ApiOperation(value = "web端上传图片", notes = "web端上传图片")
    @PostMapping("/image")
    public Result<?> uploadStoreImage(@RequestParam("iFile") MultipartFile file, @RequestParam("groupId") int groupId) throws IOException {
        YunscStoreUser storeUser = SecurityUtil.getCurrentStoreUser();
        if (groupId == -1){
            groupId = 0;
        }
        // 上传文件
        // （file 文件、groupId 组id、channel 渠道、folder 文件夹、userId 用户id、storeId 商户id）
        YunscUploadFileEntity uploadFile = uploadService.
                uploadFileEntity(file, groupId, 10, null, storeUser.getStoreUserId(), storeUser.getStoreId());
        Map<String, Object> data = new HashMap<>();
        data.put("fileInfo", uploadFile);
        return Result.ok(data);
    }

    @ApiOperation(value = "web端上传视频", notes = "web端上传视频")
    @PostMapping("/video")
    public Result<?> uploadStoreVideo(@RequestParam("iFile") MultipartFile file, @RequestParam("groupId") int groupId) throws IOException {
        YunscStoreUser storeUser = SecurityUtil.getCurrentStoreUser();
        if (groupId == -1){
            groupId = 0;
        }
        // 上传文件
        // （file 文件、groupId 组id、channel 渠道、folder 文件夹、userId 用户id、storeId 商户id）
        YunscUploadFileEntity uploadFile = uploadService.
                uploadFileEntity(file, groupId, 10, null, storeUser.getStoreUserId(), storeUser.getStoreId());
        Map<String, Object> data = new HashMap<>();
        data.put("fileInfo", uploadFile);
        return Result.ok(data);
    }



}
